﻿using System;
using System.Data;
using System.Data.SqlClient;

namespace Module.QuanTriHeThong.DanhMuc
{
    public class DMTaiKhoan : DBInteractionBase
    {
        public string TaiKhoan { get; set; }
        public string MatKhau { get; set; }
        public string HoTen { get; set; }
        public string DiaChi { get; set; }
        public string SoDienThoai { get; set; }
        public bool SuperAdmin { get; set; }
        public bool IsActive { get; set; }

        public bool Insert(DMTaiKhoan obj)
        {
            try
            {
                using (var dbcmd = new SqlCommand("tb_DMTaiKhoan_Insert", GetConnection()))
                {
                    dbcmd.CommandType = CommandType.StoredProcedure;
                    dbcmd.Parameters.Add(new SqlParameter("@TaiKhoan", obj.TaiKhoan));
                    dbcmd.Parameters.Add(new SqlParameter("@HoTen", obj.HoTen));
                    dbcmd.Parameters.Add(new SqlParameter("@DiaChi", obj.DiaChi));
                    dbcmd.Parameters.Add(new SqlParameter("@SoDienThoai", obj.SoDienThoai));
                    dbcmd.ExecuteNonQuery();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool Update(DMTaiKhoan obj)
        {
            try
            {
                using (var dbcmd = new SqlCommand("tb_DMTaiKhoan_Update", GetConnection()))
                {
                    dbcmd.CommandType = CommandType.StoredProcedure;
                    dbcmd.Parameters.Add(new SqlParameter("@TaiKhoan", obj.TaiKhoan));
                    dbcmd.Parameters.Add(new SqlParameter("@HoTen", obj.HoTen));
                    dbcmd.Parameters.Add(new SqlParameter("@DiaChi", obj.DiaChi));
                    dbcmd.Parameters.Add(new SqlParameter("@SoDienThoai", obj.SoDienThoai));
                    dbcmd.ExecuteNonQuery();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool Delete(string id)
        {
            try
            {
                using (var dbcmd = new SqlCommand("tb_DMTaiKhoan_Delete", GetConnection()))
                {
                    dbcmd.CommandType = CommandType.StoredProcedure;
                    dbcmd.Parameters.Add(new SqlParameter("@TaiKhoan", id));
                    dbcmd.ExecuteNonQuery();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public DataTable SelectById(string id)
        {
            var dt = new DataSet();
            try
            {
                using (var dbcmd = new SqlCommand("tb_DMTaiKhoan_SelectById", GetConnection()))
                {
                    dbcmd.CommandType = CommandType.StoredProcedure;
                    dbcmd.Parameters.Add(new SqlParameter("@TaiKhoan", id));
                    var dtAdapter = new SqlDataAdapter(dbcmd);
                    dtAdapter.Fill(dt);
                    return dt.Tables[0];
                }
            }
            catch (Exception ex)
            {
                throw new Exception("tb_DMTaiKhoan_SelectById: " + ex.Message);
            }
        }

        public DataTable SelectAll(DMTaiKhoan obj, int pageindex, int pagesize, out int recordcount)
        {
            var dt = new DataSet();
            try
            {
                using (var dbcmd = new SqlCommand("tb_DMTaiKhoan_SelectAll", GetConnection()))
                {
                    dbcmd.CommandType = CommandType.StoredProcedure;
                    dbcmd.Parameters.Add(new SqlParameter("@TaiKhoan", obj.TaiKhoan));
                    dbcmd.Parameters.Add(new SqlParameter("@HoTen", obj.HoTen));
                    dbcmd.Parameters.Add(new SqlParameter("@DiaChi", obj.DiaChi));
                    dbcmd.Parameters.Add(new SqlParameter("@SoDienThoai", obj.SoDienThoai));
                    dbcmd.Parameters.Add(new SqlParameter("@PageIndex", pageindex));
                    dbcmd.Parameters.Add(new SqlParameter("@PageSize", pagesize));
                    dbcmd.Parameters.Add(new SqlParameter("@RecordCount", SqlDbType.Int, 50)
                    {
                        Direction = ParameterDirection.Output
                    });
                    var dtAdapter = new SqlDataAdapter(dbcmd);
                    dtAdapter.Fill(dt);
                    recordcount = Convert.ToInt32(dbcmd.Parameters["@RecordCount"].Value);
                    return dt.Tables[0];
                }
            }
            catch (Exception ex)
            {
                throw new Exception("tb_DMTaiKhoan_SelectAll: " + ex.Message);
            }
        }

        public bool ResetMatKhau(string id)
        {
            try
            {
                using (var dbcmd = new SqlCommand("tb_DMTaiKhoan_ResetMatKhau", GetConnection()))
                {
                    dbcmd.CommandType = CommandType.StoredProcedure;
                    dbcmd.Parameters.Add(new SqlParameter("@TaiKhoan", id));
                    dbcmd.ExecuteNonQuery();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool KiemTraTaiKhoan(string taikhoan, string matkhau, bool islogin, out bool statuslogin)
        {
            var dt = new DataSet();
            try
            {
                using (var dbcmd = new SqlCommand("tb_DMTaiKhoan_CheckTaiKhoan", GetConnection()))
                {
                    dbcmd.CommandType = CommandType.StoredProcedure;
                    dbcmd.Parameters.Add(new SqlParameter("@TaiKhoan", taikhoan));
                    dbcmd.Parameters.Add(new SqlParameter("@MatKhau", matkhau));
                    dbcmd.Parameters.Add(new SqlParameter("@IsLogin", islogin));
                    dbcmd.Parameters.Add(new SqlParameter("@StatusLogin", SqlDbType.Bit)
                    {
                        Direction = ParameterDirection.Output
                    });
                    var dtAdapter = new SqlDataAdapter(dbcmd);
                    dtAdapter.Fill(dt);
                    statuslogin = Convert.ToBoolean(dbcmd.Parameters["@StatusLogin"].Value);
                    return statuslogin;
                }
            }
            catch (Exception)
            {
                statuslogin = false;
                return false;
            }
        }

        public bool DoiMatKhau(string taikhoan, string matkhau)
        {
            try
            {
                using (var dbcmd = new SqlCommand("tb_DMTaiKhoan_DoiMatKhau", GetConnection()))
                {
                    dbcmd.CommandType = CommandType.StoredProcedure;
                    dbcmd.Parameters.Add(new SqlParameter("@TaiKhoan", taikhoan));
                    dbcmd.Parameters.Add(new SqlParameter("@MatKhau", matkhau));
                    dbcmd.ExecuteNonQuery();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool Update_IsLogin(string taikhoan, bool islogin)
        {
            try
            {
                using (var dbcmd = new SqlCommand("tb_DMTaiKhoan_Update_IsLogin", GetConnection()))
                {
                    dbcmd.CommandType = CommandType.StoredProcedure;
                    dbcmd.Parameters.Add(new SqlParameter("@TaiKhoan", taikhoan));
                    dbcmd.Parameters.Add(new SqlParameter("@IsLogin", islogin));
                    dbcmd.ExecuteNonQuery();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}
